package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.entity.EqpInfoEntity;
import com.glory.good_dark.entity.StrIDIPEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: StrIpMangerAdapter
 * Author: XJH
 * Date: 2021/7/14 17:49
 * Description:
 * History:
 */
public class StrIpMangerAdapter extends BaseQuickAdapter<StrIDIPEntity.StrIDEntity, BaseViewHolder> {
    protected FreeUI_GeneralFragmentDialog dialogs;
    private FragmentManager fragmentManager;

    public StrIpMangerAdapter(@Nullable @org.jetbrains.annotations.Nullable List<StrIDIPEntity.StrIDEntity> data,FragmentManager fragmentManager) {
        super(R.layout.adapter_stripid_manger,data);
        this.fragmentManager = fragmentManager;
    }

    @Override
    protected void convert(BaseViewHolder helper, StrIDIPEntity.StrIDEntity item) {
        helper.setText(R.id.etContent,item.getSTRIP_ID());
//        setEditListener(helper.getView(R.id.etContent),item);
        CheckBox checkBox = helper.getView(R.id.checkBox);
        checkBox.setChecked(false);
        item.setChecked(false);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            item.setChecked(isChecked);
        });
        helper.setOnClickListener(R.id.etContent, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogs = FreeApi_DialogUtils.getScanDialog(mContext, "请输入料条号", s -> {
                    helper.setText(R.id.etContent,s);
                    item.setSTRIP_ID(s);
                    dialogs.dismiss();
                });
                dialogs.show(fragmentManager, "SCAN_DIALOG");
            }
        });
    }

    private void setEditListener(EditText editText, StrIDIPEntity.StrIDEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                item.setSTRIP_ID(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
